package com.lzm.controller;

import com.github.pagehelper.PageInfo;
import com.lzm.pojo.Hosregister;
import com.lzm.pojo.Menu;
import com.lzm.service.menuService;
import com.lzm.vo.QueryVo;
import com.lzm.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("resource")
public class menuController {

    @Resource
    private menuService menuService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo<Hosregister> queryByPage(Integer pageNum, Integer pageSize, String menu_name) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Menu> menuPageInfo = menuService.queryByPage(pageNum, pageSize, menu_name);
        return new ResultVo(menuPageInfo);
    }
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVo<Menu> addMenu(Menu menu) {
        int i = menuService.addMenu(menu);
        if (i == 1) {
            return new ResultVo<Menu>();
        }
        return new ResultVo<>(500, "添加失败！服务器内部异常，请稍后再试");
    }
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVo<Menu> queryById(@PathVariable("id") Integer menu_id) {
       Menu menu = menuService.queryById(menu_id);
        return new ResultVo<>(menu);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Menu> updateHosRegister(@PathVariable("id") Integer menu_id,Menu menu){
        menu.setMenu_id(menu_id);
        int i = menuService.updateMenu(menu);
        if(i==1){
            return new ResultVo<Menu>();
        }
        return new ResultVo<>(500,"更新失败！服务器内部异常，请稍后再试");
    }
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVo<Menu> deleteHosRegister(@PathVariable("id") Integer menu_id){
        int i = menuService.deleteMenu(menu_id);
        if(i==1){
            return new ResultVo<Menu>();
        }
        return new ResultVo<>(500,"删除失败！服务器内部异常，请稍后再试");
    }

    @RequestMapping(value = "dellist",method = RequestMethod.POST)
    public ResultVo<Menu> deleteList(@RequestParam("ids") String ids){
        String[] split = ids.split(",");
        int flag = 0;
        for (int i=0;i<split.length;i++){
            flag = menuService.deleteMenu(Integer.parseInt(split[i]));
        }
        if(flag<1){
            return new ResultVo<>(500,"删除失败！服务器内部异常，请稍后再试");
        }else {
            return new ResultVo<Menu>();
        }

    }
}
